<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
//require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { queryTodayTrainAnalysisWeekData } from '@/api/statistics'

const animationDuration = 1000

const weekMap = {
    "周日": 'sunday', "周一": 'monday', "周二": 'tuesday', "周三": 'wednesday', "周四": 'thursday', "周五": 'friday', "周六": 'saturday'
};

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '240px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const day = new Date().getDay()
      const weeks = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
      const xAxisWeek = []
      xAxisWeek.push(...weeks.slice(day))
      xAxisWeek.push(...weeks.slice(0, day))

      this.chart = echarts.init(this.$el, 'macarons')

      queryTodayTrainAnalysisWeekData().then(res => {
        const showData = []
        const backendData = res.data
        xAxisWeek.forEach(it => {
          let key = weekMap[it] + 'Count'
          showData.push(backendData[key] || 0)
        })

        this.chart.setOption({
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
              // lineStyle: {
              //     color: '#fff3'
              // }
            }
          },
          grid: {
            top: 35,
            left: '5%',
            right: '5%',
            bottom: '3%',
            containLabel: true
          },
          legend: {
            data: ['分析车辆数'],
            textStyle: {
              color: '#fff'
            }
          },
          xAxis: {
            type: 'category',
            data: xAxisWeek,
            boundaryGap: false,
            axisTick: {
              alignWithLabel: true
            }
          },
          yAxis: {
            type: 'value',
            axisTick: {
              show: false
            },
            max: function(value){
                return Math.ceil(value.max * 1.5);
            }
          },
          series: [
            {
              name: '分析车辆数',
              data: showData,
              type: 'line',
              animationDuration,
              areaStyle: {
                color: {
                  type: "linear",
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: "#0095ff88" // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: "#0095ff00" // 100% 处的颜色
                    }
                  ]
                }
              }
            }
          ]
        })
      })
    }
  }
}
</script>
